Ext.define('PJ.view.trainingmanagement.ConfigurationCourseEdit', {
    extend: 'AM.default.DefaultWindowForm',
    requires: ['AM.default.DefaultDateField', 
               'AM.default.DateTime', 
               'AM.default.jsonp.DefaultSubmitJsonp',
               'PJ.view.trainingmanagement.OperatingCourseList'//课程列表
               ],
    
    modal: true,// 开启不允许点击其他组件
    frame: true,
    notEdit: true,
    width: 760,
    height: 560,
    isSame: false,
    isEdit: null,

    initComponent: function(){
    	var me = this;
    	var bt = '<span style="color:red;">*</span>';
    	me.items= [{
    		xtype: "fieldset",
    		layout: 'column',
            collapsible: false, //显示三角
            defaults: {
    			xtype: 'textfield',
            	labelAlign: 'right',
            	labelWidth: 65,
            	readOnly: true,
            	fieldCls: 'noBorder',
            	columnWidth: .5
            },
    		items: [{
    			fieldLabel: '班级名称',
    			name: 'className',
            	columnWidth: 1
    		}, {
    			fieldLabel: '举办时间',
    			name: 'learnTime'
    		}, {
    			xtype: 'hidden',
    			name: 'classId'
    		}]
    	}, {
    		xtype: "fieldset",
    		layout: 'column',
            collapsible: false, //显示三角
            defaults: {
    			xtype: 'textfield',
            	labelAlign: 'right',
            	labelWidth: 65,
            	allowBlank: true, // 显示红框是否必填,默认显示,
            	padding: '0 0 5 0',
            	columnWidth: .5
            },
    		items: [{
    			fieldLabel: bt + '课程名',
    			name: 'courseName',
    			allowBlank: false,
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 50){
                			Orchid.alert('提示','该输入项最多输入50个字符！');
                		}
                	}
                },
                maxLength: 50
    		},{
				xtype: 'defaultDate',
				fieldLabel: '日期',
				name: 'speakDateStr',
				editable: false
			},{
                xtype: 'combo',
                fieldLabel: bt + '课程类型',
                name: 'speakway',
                blankText: "请选择",
                allowBlank: false,
                editable: false,
                queryMode: 'local',//'remote',
                store: Ext.create("Ext.data.ArrayStore", {
                    singleton: true,
                    fields: [{
                        name: 'value',
                        mapping: "dataCode",
                        type: 'string'
                    }, {
                        name: 'text',
                        mapping: "dataName",
                        type: 'string'
                    }],
                    proxy: {
                        type: gj_onlineLearning_constant.loadType,
                        url: me.grid.store.courseTypeUrl + '?typeCode=skfs',
                        reader: Ext.create('Ext.data.reader.Json', {
                            type: 'json',
                            root: 'data',
                            successProperty: 'success'
                        })
                    },
                    autoLoad: true
                }),
                valueField: "value",
                displayField: "text",
                maxLength: 20
            }, {
                xtype: "combo",
                fieldLabel: "时间",
                name: 'speakTime',
                queryMode: 'local',//'remote',
                store: Ext.create("Ext.data.ArrayStore", {
                    singleton: true,
                    fields: [{
                        name: 'value',
                        mapping: "dataCode",
                        type: 'string'
                    }, {
                        name: 'text',
                        mapping: "dataName",
                        type: 'string'
                    }],
                    proxy: {
                        type: gj_onlineLearning_constant.loadType,
                        url: me.grid.store.courseTimeTypeUrl + '?typeCode=speakTime',
                        reader: Ext.create('Ext.data.reader.Json', {
                            type: 'json',
                            root: 'data',
                            successProperty: 'success'
                        })
                    },
                    autoLoad: true
                }),
                valueField: 'value',
                displayField: 'text',
                editable: false
            },{
    			fieldLabel: '授课教师',
    			name: 'speaker',
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 20){
                			Orchid.alert('提示','该输入项最多输入20个字符！');
                		}
                	}
                },
                maxLength: 50
    		}, {
    			fieldLabel: '教师单位',
    			name: 'speakdept',
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 50){
                			Orchid.alert('提示','该输入项最多输入50个字符！');
                		}
                	}
                },
                maxLength: 50
    		}, {
    			fieldLabel: '职务职称',
    			name: 'speakDuty',
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 30){
                			Orchid.alert('提示','该输入项最多输入30个字符！');
                		}
                	}
                },
                maxLength: 50
    		}, 
    		me.searchButtonPanel(), //按钮
    		{
                xtype: 'pfOperatingCourseList',
                columnWidth: 1,
                classId: me.classId,
            	padding: '0 0 0 0',
                height: 271
            }]
    	}];
        me.buttons = [{
            text: '关闭',
            scope: this,
            handler: this.close
        }];
        me.callParent(arguments);
    },
    listeners: {
    	close: function(win){
    		 this.grid.store.load();
    	 }
    },
	//查询和重置按钮组合的panel
	searchButtonPanel : function() {
		var me = this;
		var searchButtons = me.searchButtons;
		var panel = Ext.create("Ext.container.Container", {
			layout : "hbox",
			padding:"5 7 10 5",
			items : [{
				xtype : 'tbfill'
			}, me.saveButton(), {
				xtype : 'tbspacer',
				width : "22px"
			}, me.resetButton()]
		});
		Ext.each(searchButtons, function(btn) {
			panel.add({
				xtype : 'tbspacer',
				width : "22px"
			}, btn);
		});
		return panel;
	},
	//保存按钮
	saveButton : function() {
		var form = this;
		var button = Ext.create('Ext.button.Button', {
			text : '保存',
			width : 40,
			itemId : "save",
			listeners : {
				click : function() {
					form.onSaveClick(this);
				}
			}
		});
		return button;
	},
	//重置按钮
	resetButton : function() {
		var form = this;
		var button = Ext.create('Ext.button.Button', {
			text : '重置',
			width : 40,
			itemId : "resetButton",
			listeners : {
				click : function() {
					form.onResetClick(this);
				}
			}
		});
		return button;
	},
	//保存
	onSaveClick: function(){
		var win = this;
		var form = win.down('form');
		var baseForm = form.getForm();
		if (!baseForm.isValid()) {
			return;
		}
		if(gj_onlineLearning_constant.loadType == "jsonp"){
			form.getForm().doAction('submitJsonp',{
				submitEmptyText:false,
				url: win.grid.store.courseAddUrl,
				params: {},
				success: function(f, action){
					if (action.result.success) {
						Ext.Msg.alert('消息', "新增成功！", function(){
							var gridPanel = win.down('grid');
							var store = gridPanel.getStore();
							store.reload();
							baseForm.findField("courseName").reset();
							baseForm.findField("searchDate").reset();
							baseForm.findField("speakway").reset();
							baseForm.findField("time").reset();
							baseForm.findField("teacher").reset();
							baseForm.findField("techerUnit").reset();
							baseForm.findField("teacherPost").reset();
							baseForm.findField("courseId").reset();
							win.saveToken();
						});
					}
				},
				failure: function(f, action){
					if (action.result) {
						Ext.Msg.alert('错误', action.result.msg || "操作失败!");
					}
				}
			});
		}else if(gj_onlineLearning_constant.loadType == "ajax"){
			win.submit({
				submitEmptyText:false,
				url: win.grid.store.courseAddUrl,
				params: {},
				success: function(f, action){
					if (action.result.success) {
						Ext.Msg.alert('消息', '新增成功！', function(){
							var gridPanel = win.down('grid');
							var store = gridPanel.getStore();
							store.reload();
							baseForm.findField("courseName").reset();
							baseForm.findField("speakDateStr").reset();
							baseForm.findField("speakway").reset();
							baseForm.findField("speakTime").reset();
							baseForm.findField("speaker").reset();
							baseForm.findField("speakDuty").reset();
							baseForm.findField("speakdept").reset();
							win.saveToken();
						});
					}
				},
				failure: function(f, action){
					if (action.result) {
						Ext.Msg.alert('错误', action.result.msg || "系统出错,请联系管理员!");
					}
				}
			});
		}
	},

	//默认取消按钮点击事件
	onResetClick: function(btn) {
		var win = this;
		var form = win.down('form');
		var baseForm = form.getForm();
		baseForm.findField("courseName").reset();
		baseForm.findField("speakDateStr").reset();
		baseForm.findField("speakway").reset();
		baseForm.findField("speakTime").reset();
		baseForm.findField("speaker").reset();
		baseForm.findField("speakDuty").reset();
		baseForm.findField("speakdept").reset();
//		this.close();
	},

	saveToken: function(){
        var form = this.down("form");
        Ext.Ajax.request({
			url: tokenUrl,
            success: function(response){
                var data = response.responseText;
                var token = Ext.decode(data, true);
                form.getForm().findField("springmvc.TOKEN").setValue(token);
            }
        });
    }
    
});
